<?php
// no direct access
defined( '_AppMiage' ) or die( 'Restricted access' );
if(isEtudiant()){
    //include header
    include_once (DS.'include/header.php');
    //include menu
    include_once (DS.'include/menu.php');

    $showOK = false;
    $showError = false;

    if(isset($_POST['submit']))
    {
        // variable qui va nous servir pour savoir s'il y a a eu une erreur ;)
        $form = true;
        $errMSG = array();

        if(isset($_POST['name']) && !empty($_POST['name']))
        {
            $tmp = $global_member->setNomUtilisateur($_POST['name']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['nom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['prenom']) && !empty($_POST['prenom']))
        {
            $tmp = $global_member->setPrenomUtilisateur($_POST['prenom']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['prenom'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['mail']) && !empty($_POST['mail']))
        {
            $tmp = $global_member->setEmailUtilisateur($_POST['mail']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['mail'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['add1']) && !empty($_POST['add1']))
        {
            $tmp = $global_member->setAdresse1($_POST['add1']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['add1'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['add2']) && !empty($_POST['add2']))
        {
            $tmp = $global_member->setAdresse2($_POST['add2']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['add2'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['ville']) && !empty($_POST['ville']))
        {
            $tmp = $global_member->setVille($_POST['ville']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['ville'] = $tmp['errmsg'];
                $form = false;
            }
        }

        if(isset($_POST['cp']) && !empty($_POST['cp']))
        {
            $tmp = $global_member->setCP($_POST['cp']);
            if($tmp['boolOk'] === false)
            {
                $errMSG['cp'] = $tmp['errmsg'];
                $form = false;
            }
        }
        if(isset($_POST['old_pwd']) && !empty($_POST['old_pwd']) &&
            isset($_POST['pwd']) && !empty($_POST['pwd']) &&
            isset($_POST['pwd2']) && !empty($_POST['pwd2']) )
        {
            if(md5($_POST['old_pwd']) == $global_member->getPass())
            {
                if($_POST['pwd'] == $_POST['pwd2'])
                {
                    $tmp = $global_member->setPassword($_POST['pwd']);
                    if($tmp['boolOk'] === false)
                    {
                        $errMSG['pwd'] = $tmp['errmsg'];
                        $form = false;
                    }
                }
                else
                {
                    $form = false;
                }
            }
            else
            {
                $form = false;
            }
        }

        if($form === true)
        {
            $global_member->update();
            if($global_member->requeteOk === true)
            {
                $showOK = true;
            }
            else
            {
                $showError = true;
            }
        }
    }

    ?>
    <div id="content">
        <div class="post">
            <h1 class="title">Modifier mon Profil</h1>
            <?php if($showError): ?>
                <div class="error">Une erreur est survenue. Veuillez re-essayer ult&egrave;rieurement.</div>
            <?php endif; ?>
            <?php if($showOK): ?>
                <div class="success">La sauvegarde s'est correctement effectu&eacute;e.</div>
            <?php endif; ?>
            <div class="entry">
                <form action="<?php echo URL; ?>index.php?m=profil_etudiant" method="post" id="profil_etudiant">
                    <fieldset>
                        <legend>Informations Personnelles</legend>
                        <label for="name">Nom : </label><br />
                        <input class="title" type="text" name="name" id="name" value="<?php echo $global_member->getNomUtilisateur(); ?>"/><br />
                        <?php if(isset($errMSG['nom']) && !empty($errMSG['nom'])){ ?><p class="error"><?php echo $errMSG['nom']; ?></p><?php } ?>
                        <br />
                        <label for="prenom">Pr&eacute;nom : </label><br />
                        <input class="title" type="text" name="prenom" id="prenom" value="<?php echo $global_member->getPrenomUtilisateur(); ?>"/><br />
                        <?php if(isset($errMSG['prenom']) && !empty($errMSG['prenom'])){ ?><p class="error"><?php echo $errMSG['prenom']; ?></p><?php } ?>
                        <br />
                        <label for="mail">Email : </label><br />
                        <input class="title" type="text" name="mail" id="mail" value="<?php echo $global_member->getEmailUtilisateur(); ?>"/><br />
                        <?php if(isset($errMSG['mail']) && !empty($errMSG['mail'])){ ?><p class="error"><?php echo $errMSG['mail']; ?></p><?php } ?>
                        <br />
                        <label for="old_pwd">Ancien mot de passe : </label><br />
                        <input class="title" type="password" name="old_pwd" id="old_pwd" value=""/><br />
                        <br />
                        <label for="pwd">Mot de passe : </label><span style="font-size:10px;">entre 6 et 15 caract&egrave;res</span><br />
                        <input class="title" type="password" name="pwd" id="pwd" value="" onkeyup="verifLength(this.value,'length');" />&nbsp;&nbsp;<span id="length"></span><br />
                        <br />
                        <label for="pwd2">Retaper le mot de passe : </label><br />
                        <input class="title" type="password" name="pwd2" id="pwd2" value="" onkeyup="verifPass($('#pwd').val(),$('#pwd2').val() , 'result');" />&nbsp;&nbsp;<span id="result"></span><br />
                        <br />
                        <br />
                    </fieldset>
                    <fieldset>
                        <legend>Contact</legend>
                        <label for="add1">Adresse : </label><br />
                        <input class="title" type="text" name="add1" id="add1" value="<?php echo $global_member->getAdresse1(); ?>"/><br />
                        <?php if(isset($errMSG['add1']) && !empty($errMSG['add1'])){ ?><p class="error"><?php echo $errMSG['add1']; ?></p><?php } ?>
                        <br />
                        <label for="add2">Adresse compl&eacute;mentaire : </label><br />
                        <input class="title" type="text" name="add2" id="add2" value="<?php echo $global_member->getAdresse2(); ?>"/><br />
                        <?php if(isset($errMSG['add2']) && !empty($errMSG['add2'])){ ?><p class="error"><?php echo $errMSG['add2']; ?></p><?php } ?>
                        <br />
                        <label for="cp">Code Postal : </label><br />
                        <input class="title" type="text" name="cp" id="cp" value="<?php echo $global_member->getCP(); ?>"/><br />
                        <?php if(isset($errMSG['cp']) && !empty($errMSG['cp'])){ ?><p class="error"><?php echo $errMSG['cp']; ?></p><?php } ?>
                        <br />
                        <label for="ville">Ville : </label><br />
                        <input class="title" type="text" name="ville" id="ville" value="<?php echo $global_member->getVille(); ?>"/><br />
                        <?php if(isset($errMSG['ville']) && !empty($errMSG['ville'])){ ?><p class="error"><?php echo $errMSG['ville']; ?></p><?php } ?>
                        <br />
                    </fieldset>
                <div align="center">
                    <input class="button green" type="submit" name="submit" value="Sauver" />
                </div>
                </form>
            </div>
        </div>
    </div>
    <?php
    include_once (DS.'include/sidebar.php');

    include_once (DS.'include/footer.php');
}
else
{
    redirectTo();
}
?>